<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>成绩</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div class="layui-form" style="margin: 20px">
    <h1>成绩列表</h1>
    <div style="margin: 20px 0px">
        查询<input class="inp" id="name" type="text"/><button class="btn" id="search">搜索</button><button id="add" class="btn">新增</button>
    </div>
    <table class="layui-table"><colgroup>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="150"/>
        <col width="220"/>
    </colgroup>
        <thead>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>语文</th>
            <th>数学</th>
            <th>英语</th>
            <th>历史</th>
            <th>地理</th>
            <th>政治</th>
            <th>物理</th>
            <th>化学</th>
            <th>生物</th>
            <th>体育</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tableData">

        </tbody>
    </table>
    <div style="margin-top: 20px">
        <button class="btn" id="up">上一页</button>
        当前页：<span id="cur"></span> 总页数：<span id="count"></span>
        <button class="btn" id="down">下一页</button>
    </div>
</div>
</body>
</html>
<script>

    $(function(){
        getdata(1)
    });

    $("#search").click(function(){
        getdata(1)
    })
    $("#add").click(function(){
        location.href = "add"
    })
   function updatedata(data){
        location.href='update?id='+data;
   }
    function deletedata(data){
        $.ajax({
            url: "/result/delete",// The URL for the request
            contentType : "application/json",
            data: {id : data},
            type: "GET",// Whether this is a POST or GET request
        }).done(function(res) {
            if (res.code==200){
                alert("删除成功")
                getdata(1)
            } else {
                alert(res.message);
            }
        });
    }
    $("#up").click(function(){
        var cur = $("#cur").text();
        if (cur==1){
            alert("没有上一页")
            return false
        }
        getdata(cur-1)
    })
    $("#down").click(function(){
        var cur = $("#cur").text();
        var count = $("#count").text();
        if (cur==count){
            alert("没有下一页")
            return false
        }
        getdata(cur+1)
    })
    function getdata(page){
        $("#tableData").empty();
        $.ajax({
            url: "/result/list",// The URL for the request
            contentType : "application/json",
            data: {page:page, size: 10,name:$("#name").val()},
            type: "GET",// Whether this is a POST or GET request
        }).done(function(res) {
            if (res.code === 200){
                var data = ""
                $("#cur").text(res.data.pageNum)
                $("#count").text(res.data.lastPage)
                console.log(res.data.list)
                $.each(res.data.list,function(i,item){
                    data +=`<tr>`+
                        `<td>`+(i+1)+`</td>`+
                        `<td>`+ item.name+`</td>`+
                        `<td>`+ item.yuwen+`</td>`+
                        `<td>`+ item.shuxue+`</td>`+
                        `<td>`+ item.yingyu+`</td>`+
                        `<td>`+ item.lishi+`</td>`+
                        `<td>`+ item.dili+`</td>`+
                        `<td>`+ item.zhengzhi+`</td>`+
                        `<td>`+ item.wuli+`</td>`+
                        `<td>`+ item.huaxue+`</td>`+
                        `<td>`+ item.shengwu+`</td>`+
                        `<td>`+ item.tiyu+`</td>`+
                        `<td><button onclick="updatedata(`+item.id+`)" class="btn">修改</button><button onclick="deletedata(`+item.id+`)" class="btn">删除</button></td>`+
                        `</tr>`;
                });
                $("#tableData").append(data)
            } else {
                alert(res.message)
            }
        });
    }
</script>

<style>
    .layui-table{
        border-collapse: collapse;
        text-align: center;
    }
    .layui-table,th,td{
        border: 1px solid black;
    }
    .btn{
        line-height: 28px;
        color: #67C23A;
        width: 80px;
        font-size: 18px;
        margin: 0px 10px;
    }
    .inp{
        line-height: 32px;
        font-size: 20px;
    }
</style>